package com.wilmer.task;

import com.wilmer.common.core.redis.RedisCache;
import com.wilmer.bean.TokenBean;
import com.wilmer.utils.AccessTokenUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component("GetAccessToken")
public class AccessTokenTask {

    @Resource
    private AccessTokenUtil accessTokenUtil;
    @Resource
    private RedisCache redisCache;

    private TokenBean tokenBean;

    public void getAccessToken(){

        //检查缓存中是否有值
        Object access_token = redisCache.getCacheObject("access_token");

        if (access_token==null) {
            tokenBean= accessTokenUtil.getToken();
            redisCache.setCacheObject("access_token",tokenBean.getAccess_token(),7200, TimeUnit.SECONDS);
            System.out.println("access_token缓存成功");
        }else {
            if(redisCache.getExpire("access_token")<=600L){
                tokenBean= accessTokenUtil.getToken();
                redisCache.deleteObject("access_token");
                redisCache.setCacheObject("access_token",tokenBean.getAccess_token(),7200, TimeUnit.SECONDS);
                System.out.println("access_token即将过期，以获取新的access_token");
            }else {
                System.out.println("access_token监测正常");
            }
        }



    }


}
